using System;
using MetaObjects.Contracts;
using MetaObjects.Core;
using Microsoft.SharePoint;

namespace MetaObjects.SharePoint
{
	public class SharePointField : MetaField<SPListItem, object>
	{
		public SharePointField(string internalName) : base(internalName)
		{
		}

		public override Action<SPListItem, object> SetField
		{
			get { return (item, value) => item[InternalName] = value; }
		}

		public override Func<SPListItem, object> GetField
		{
			get { return item => item[InternalName]; }
		}
	}

	public class SharePointField<TValue> : IMetaField<SPListItem, TValue>
	{
		object IEntity.Key { get { return InternalName; } }
		public string InternalName { get; private set; }
		public string DisplayName { get; set; }
		public string Description { get; set; }
		public bool Required { get; set; }
		public bool ReadOnly { get; set; }
		object IMetaField.DefaultValue
		{
			get { return DefaultValue;  }
			set { DefaultValue = (TValue)value; }
		}
		public TValue DefaultValue { get; set; }

		Action<object, object> IMetaField.SetField { get { return (entity, value) => SetField((SPListItem)entity, (TValue)value); } }
		Func<object, object> IMetaField.GetField { get { return entity => GetField((SPListItem)entity); } }
		Action<object, TValue> IMetaField<TValue>.SetField { get { return (entity, value) => SetField((SPListItem)entity, value); } }
		Func<object, TValue> IMetaField<TValue>.GetField { get { return entity => GetField((SPListItem)entity); } }

		public Action<SPListItem, TValue> SetField { get; set; }
		public Func<SPListItem, TValue> GetField { get; set; }

		public SharePointField(string internalName)
		{
			InternalName = internalName;
		}
	}


}